<!-- 快运批次管理 -->
<template>
<div class="content-box">
  <FilterBox
    :labelWidth="120"
    :filterItemList="filterItemList"
    :filterData="filterData"
    @search="search"
    @clear="clear"
    @keep="keepHeaderList"
  >
  </FilterBox>
  <MyTable
    ref="table_ref"
    :tableColumn="tableColumn"
    :tableData="tableData"
    @setOrder="setOrder"
    @sendTabClick="setCurrent"
    >
    <template v-slot:content="scope">
      <span>{{ scope.row[scope.config.prop] }}</span>
    </template>
  </MyTable>
  <!-- 分页组件 -->

  <Pagination
    :currentPage="pageinfo.page"
    :pageSize="pageinfo.limit"
    :total="pageinfo.number_all"
    :page_all="pageinfo.page_all"
    @pagination="SetPagination"
    @sendPushButton="pushButton"
  >
  </Pagination>

  <Generate
    @success="getTableData"
    ref="generate"
  ></Generate>
  <SetStatus
    @success="getTableData"
    ref="setStatus"
  ></SetStatus>
  <TrajectoryAdd
    @success="getTableData"
    ref="TrajectoryAdd"
  ></TrajectoryAdd>
</div>
</template>

<script>
import Generate from "./ExpressBatch_generate";
import SetStatus from "./ExpressBatch_setStatus";
import TrajectoryAdd from "./ExpressBatch_trajectoryAdd";
export default {
  components: {
    Generate,
    SetStatus,
    TrajectoryAdd,
  },
  data(){
    return{
      filterItemList:[
        // input输入框
        { label:this.$t('ExpressBatchLang.batch_name'),width:200, filterKey: 'allocation_number_alias', filterType: 'SearchInput', placeholderStr: null },
         // 日期选择器
        { label:this.$t('ExpressBatchLang.batch_date'),width:200, filterKey: 'allocation_day', filterType: 'SearchDate' },
        // 下拉框
        { label:this.$t('ExpressOrderManagementLang.Origin'),width:200, filterKey: 'start_local', filterType: 'SearchSelect', placeholderStr: null, filterOptions: {options:[],label:'name_current',value:'id'} },
      ],
      filterData:{
        page: "",
        limit: "30",
        order: "",

        allocation_number_alias: "",
        allocation_day:[],
        allocation_day1: "",
        allocation_day2: "",
        start_local:'',
      },
      localoptionsStr:[],
      tableColumn: [
        {
          label: "",
          prop: "table_code",
          width:40,
        },
        {
          checked: true,
          label: this.$t('ExpressBatchLang.batch_name'),
          prop: "allocation_number_alias",
          width: 100,
          order:"",
        },
        {
          checked: true,
          label: this.$t('ExpressBatchLang.batch_num'),
          prop: "allocation_number",
          width: 200,
          order:"",
        },
        {
          checked: true,
          label: this.$t('ExpressBatchLang.batch_date'),
          prop: "allocation_day",
          width: 100,
          order:"",
        },
        {
          checked: true,
          label: this.$t('ExpressOrderManagementLang.Origin'),
          prop: "start_local_name",
          width: 100,
          order:"",
        },
        {
          checked: true,
          label: this.$t('SubAccountManagementLang.state'),
          prop: "state_name",
          width: 100,
          order:"",
        },
        {
          checked: true,
          label: this.$t('ExpressDeliveryManagementLang.Number_packages'),
          prop: 'package_num',
          width: 100,
          order:"",
        },
        {
          checked: true,
          label: this.$t('ExpressCargoManagementLang.weight'),
          prop: "weight",
          width: 100,
          order:"",
        },
        {
          checked: true,
          label: this.$t('ExpressCargoManagementLang.volume'),
          prop: "volume",
          width: 100,
          order:"",
        },
      ],
      tableData: [],

      pageinfo: {
        page: 1,
        page_all: 1,
        limit: 30,
        number_all: 0,
      },
      sendDataObj:{},
    }
  },
  computed: {
      tabsId() {
          return this.$store.state.tabsId;
      }
  },
  mounted(){
    this.getHeaderList()
    this.getOptionsList()
    this.getTableData()
  },
  methods:{
    // 获取下拉单列表
    async getOptionsList() {
      await this.$get("visibleAddress",{type:1}).then((res) => {
        this.localoptionsStr = res.data.start_local;
      });
      this.filterItemList=[
        // input输入框
        { label:this.$t('ExpressBatchLang.batch_name'),width:200, filterKey: 'allocation_number_alias', filterType: 'SearchInput', placeholderStr: null },
         // 日期选择器
        { label:this.$t('ExpressBatchLang.batch_date'),width:200, filterKey: 'allocation_day', filterType: 'SearchDate' },
         // 下拉框
        { label:this.$t('ExpressOrderManagementLang.Origin'),width:200, filterKey: 'start_local', filterType: 'SearchSelect', placeholderStr: null, filterOptions: {options:this.localoptionsStr,label:'name_current',value:'id'} },
      ]
    },
    //查询
    search(){
      this.getTableData()
    },
    //清空
    clear(){
      this.filterData.allocation_number_alias= "",
      this.filterData.allocation_day=[],
      this.filterData.allocation_day1= "",
      this.filterData.allocation_day2= "",
      this.filterData.start_local= "",
      this.getTableData()
    },
    // 获取表头组
    getHeaderList(){
      this.$get("get_th",{menu_id:this.tabsId}).then((res) => {
        if(!!res.data.th_json){
          this.tableColumn=JSON.parse(res.data.th_json)
        }
      });
    },
    //保存表头表头组
    keepHeaderList(){
        let params = new FormData();
        params.append("menu_id", this.tabsId);
        params.append("th_json", JSON.stringify(this.tableColumn));
        this.$postForm("save_th", params).then((res) => {
          
        });
    },
    // 点击排序
    setOrder(data){
      this.filterData.order=data
      this.getTableData()
    },
    // 更改分页
    SetPagination(data){
      this.filterData.page=data.page
      this.filterData.limit=data.limit
      this.pageinfo.page=data.page
      this.pageinfo.limit=data.limit
      this.getTableData()
    },
    pushButton(route) {
        if (route == "generate") {
          if (JSON.stringify(this.sendDataObj) == "{}") {
            this.$refs.generate.init();
          } else {
            this.$refs.generate.init(this.sendDataObj.allocation_number);
          }
        }else if (route == "del") {
          if (JSON.stringify(this.sendDataObj) == "{}") {
            this.$message.warning(this.$t('common.message_warn_text'));
          } else {
            this.$confirm(this.$t('ExpressCargoManagementLang.ConfirmDel_text'), this.$t('common.prompt'), {
              confirmButtonText: this.$t('common.confirm'),
              cancelButtonText: this.$t('common.cancel'),
            }).then(() => {
              let params = new FormData();
                params.append("allocation_number", this.sendDataObj.allocation_number);
              this.$postForm('delAllocationOrder',params).then((res) => {
                if(res.success){
                  this.$message.success(res.msg);
                  this.getTableData()
                }
              })
            }).catch(() => {});
          }
        }else if (route == "Outbound") {
          if (JSON.stringify(this.sendDataObj) == "{}") {
            this.$message.warning(this.$t('common.message_warn_text'));
          } else {
            if(this.sendDataObj.state!==0){
              this.$message.warning(this.$t('ExpressBatchLang.message_warn_text1'));
              return
            }
            this.$confirm(this.$t('ExpressCargoManagementLang.ConfirmTransport_text'), this.$t('common.prompt'), {
              confirmButtonText: this.$t('common.confirm'),
              cancelButtonText: this.$t('common.cancel'),
            }).then(() => {

              let params = new FormData();
                params.append("allocation_number", this.sendDataObj.allocation_number);
                params.append("track_at", new Date(this.sendDataObj.allocation_day).getTime()/1000);
                params.append("state", 4);
              this.$postForm('editAllocationState',params).then((res) => {
                if(res.success){
                  this.$message.success(res.msg);
                  this.getTableData()
                }
              })
            }).catch(() => {});
          }
        }else if (route == "TrajectoryAdd") {
          if (JSON.stringify(this.sendDataObj) == "{}" || this.sendDataObj == null) {
            this.$message.warning(this.$t('common.message_warn_text'));
          } else {
            this.$refs.TrajectoryAdd.init(this.sendDataObj)
          }
        }else if (route == "transportTo") {
          if (JSON.stringify(this.sendDataObj) == "{}") {
            this.$message.warning(this.$t('common.message_warn_text'));
          } else {
            if(this.sendDataObj.state!==4&&this.sendDataObj.state!==7){
              this.$message.warning(this.$t('ExpressBatchLang.message_warn_text2'));
              return
            }
            this.$refs.setStatus.init(this.sendDataObj,'运达')
          }
        }
    },
    // 获取表格数据
    getTableData(){
      this.$get("getAllocationOrderList", this.filterData).then((res) => {
        this.tableData = res.data;
        this.pageinfo = res.pageinfo;
        this.pageinfo.limit=Number(this.pageinfo.limit)
        this.sendDataObj={}
      });
    },
    setCurrent(data){
      console.log(data)
      // this.sendDataObj=data
      if(this.tableData.length>0){
            if(this.tableData[data.index].allocation_number==this.sendDataObj.allocation_number){
                this.$refs.table_ref.$refs.tableRef.clearCurrentRow()
                this.sendDataObj={}
            }else{
                this.sendDataObj=this.tableData[data.index]
            }
        }
    },
  }
}
</script>
<style scoped lang="less">
.tableBox {
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
</style>